//
//  BubbleButtonView.m
//  QT
//
//  Created by 甘世清 on 2025/1/5.
//

#import "BubbleButtonView.h"

@implementation BubbleButtonView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.layer.borderColor = SQHexColor(@"#000000", 0.2).CGColor;
        self.layer.borderWidth = 0.4;
        self.backgroundColor = SQHexColor(@"#DA70D6", 0.2);
        self.layer.cornerRadius = 40;
        self.titleLabel.font = FONT_BOLD(12);
        [self setTitleColor:SQColor333333 forState:UIControlStateNormal];
        self.adjustsImageWhenHighlighted = NO;
        [self addSubview:self.bgImage];
        [self.bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.bgImage bringSubviewToFront:self];
        CABasicAnimation *animation1 = [CABasicAnimation animation];
         animation1.keyPath = @"position.y";
        animation1.duration =arc4random() % 2 + 2;
         animation1.autoreverses = YES;
         animation1.byValue = @(10);
         animation1.repeatCount = MAXFLOAT;
        animation1.removedOnCompletion = NO;
         [self.layer addAnimation:animation1 forKey:@"animation1"];
     
    }
    return self;
}
- (UIImageView *)bgImage{
    if (!_bgImage) {
        _bgImage = [[UIImageView alloc]init];
        _bgImage.image = SQGetImage(@"气泡");
    }
    return _bgImage;
}
-(void)move:(CGRect)rectF arr:(NSArray *)list{
    
    //向角度的方向移动，偏移圆心
    self.centerX += self.mVx;
    self.centerY += self.mVy;
    
    /** 边界检测 **/
    float f = self.right - rectF.size.width;
    if(f > 0) { //碰右
        self.mVx = - fabsf(self.mVx);
        self.centerX = rectF.size.width - self.width*0.5 - f;
    }
    
    f = self.left;
    if(f < 0) { //碰左
        self.mVx = fabs(self.mVx);
        self.centerX = self.width*0.5 - f;
    }
    
    f = self.bottom - rectF.size.height;
    if(f > 0) { //碰下
        self.mVy = - fabs(self.mVy);
        self.centerY = rectF.size.height - self.width*0.5 - f;
    }
    
    f = self.centerY - self.width*0.5;
    if(f < 0) { //碰上
        self.mVy = fabs(self.mVy);
        self.centerY = self.width*0.5 - f;
    }
    
    // TODO 速度渐变
    // 碰撞检测
    for(BubbleButtonView *bubble in list) {
        if(bubble == self) {
            continue;
        }
        float dx = fabs(self.centerX - bubble.centerX);
        float dy = fabs(self.centerY - bubble.centerY);
        // 距离
        float dis = (float) sqrt(dx * dx + dy * dy);
        
        if(dis == 0) dis = 1;
        
        float d = self.width*0.5 + bubble.width*0.5 - dis;
        
        if(d >= 0) { // 距离小于半径和
            float py1 = (dy * dy) * bubble.mVy / dis / dis
            + (dy * dx) * bubble.mVx / dis / dis;
            float px1 = (dx * dx) * bubble.mVx / dis /dis
            + (dy * dx) * bubble.mVy / dis / dis;
            
            float py2 = (dy * dy) * self.mVy / dis / dis
            + (dy * dx) * self.mVx / dis / dis;
            float px2 = (dx * dx) * self.mVx / dis /dis
            + (dx * dy) * self.mVy / dis / dis;
            
            self.mVx += px1;
            self.mVx -= px2;
            self.mVy += py1;
            self.mVy -= py2;
            
            bubble.mVx -= px1;
            bubble.mVx += px2;
            bubble.mVy -= py1;
            bubble.mVy += py2;
            
            if(d > 0) {
                float dx2 = d * (bubble.centerX -  self.centerX) / dis;
                float dy2 = d * (bubble.centerY - self.centerY) / dis;
                bubble.centerX += dx2;
                bubble.centerY += dy2;
            }
        }
    }
    
    
    
}

@end
